/**
 * @author Tobias Boehm
 *
 * 11.05.2011
 */
package org.anddev.andengine.extension.tbsengine.message.server.preparematch;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import org.anddev.andengine.extension.tbsengine.match.AbsServerMatchPreparation;
import org.anddev.andengine.extension.tbsengine.message.ICopyable;
import org.anddev.andengine.extension.tbsengine.message.server.AbsServerMatchMessage;
import org.anddev.andengine.extension.tbsengine.message.server.DefaultTBSserverMessageFlags;

/**
 * This server message should be used to send a promotion message to a player 
 * for an open match preparation.
 * 
 * @author Tobias Boehm
 *
 */
public class ServerMatchpreparationPromotionMessage extends AbsServerMatchMessage implements DefaultTBSserverMessageFlags, ICopyable<ServerMatchpreparationPromotionMessage> {

	private int mAvailableSlots = -1;
	private String mMapID = "";
	private int mMapVersion = -1;
	
	/* (non-Javadoc)
	 * @see org.anddev.andengine.extension.multiplayer.protocol.adt.message.IMessage#getFlag()
	 */
	@Override
	public short getFlag() {
		
		return FLAG_SERVER_MATCHPREPARATION_PROMOTION;
	}
	
	@Override
	protected void onReadTransmissonMatchData(DataInputStream pDataInputStream)
			throws IOException {

		this.mMapID = pDataInputStream.readUTF();
		
		this.mMapVersion = pDataInputStream.readInt();
		
		this.mAvailableSlots = pDataInputStream.readInt();
	}

	@Override
	protected void onWriteTransmissonMatchData(
			DataOutputStream pDataOutputStream) throws IOException {

		pDataOutputStream.writeUTF(this.mMapID);
		
		pDataOutputStream.writeInt(this.mMapVersion);
		
		pDataOutputStream.writeInt(this.mAvailableSlots);
	}

	/**
	 * @return the mAvailableSlots
	 */
	public int getAvailableSlots() {
		return mAvailableSlots;
	}

	/**
	 * @return the mMapID
	 */
	public String getMapID() {
		return mMapID;
	}

		
	public void setMatchPreparation(final AbsServerMatchPreparation pMatch){
	
		super.setMatchPreparation(pMatch);
		
		this.mAvailableSlots = pMatch.getOpenSlotsCount();
		
		this.mMapVersion = pMatch.getMapOverview().getMapVersion();
		
		this.mMapID = pMatch.getMapOverview().getMapID();
	}
		
	/**
	 * @return the mMapVersion
	 */
	public int getMapVersion() {
		return mMapVersion;
	}

	@Override
	public ServerMatchpreparationPromotionMessage copy() {
	
		final ServerMatchpreparationPromotionMessage copy = new ServerMatchpreparationPromotionMessage();
		
		copy.mAvailableSlots = this.mAvailableSlots;
		copy.mMapID = this.mMapID;
		copy.mMapVersion = this.mMapVersion;
		copy.mMatchID = this.mMatchID;
		
		return copy;
	}
}
